
import tkinter as tk
from tkinter import ttk
from tkinter import Menu
from tkinter import Frame
from tkinter import filedialog
import pandas as pd


WINDOW_TITLE = '工序管理系统'

MAIN_PAGE_SIZE = '1080x700'

MENU_LIST = [
	[
		'工序1',
		('工序1查询', 1, ),
		('工序1插入', 2, ),
	],
	[
		'工序2',
		('工序2插入', 3, ),
		('工序2查询', 4, ),
	],
]


SERCH_INFO_LABEL_0 = """
公告：这个是XZXXXXXX生成的XXXX功能，有着XXXXXX的功能
如何使用？ 
第一步XXXX;
第二步XXXXXX;
更多请咨询电话12345678"""
SERCH_INFO_LABEL_1 = ""

SERCH_INFO_LABEL_2 = "查询"
SERCH_INPUT_ENTRY_WIDTH = 30

SEARCH_INFO_TABLE_COLUMNS = ('ID', '姓名', '年龄', '体重')
SEARCH_INFO_TABLE_COLUMN_WIDTH = 100

INPUT_INFO_LABEL_0 = "公告：这个是XZXXXXXX生成的XXXX功能，有着XXXXXX的功能，如何使用？ 第一步XXXX，第二步XXXXXX，更多请咨询电话12345678"
INPUT_INFO_LABEL_1 = "运输箱二维码"
INPUT_INFO_LABEL_2 = "卷料二维码"
INPUT_INFO_LABEL_3 = "机箱参数1"
INPUT_INFO_LABEL_4 = "机箱参数2"
INPUT_INFO_LABEL_5 = "机箱参数3"
INPUT_INFO_LABEL_6 = "机箱参数4"
INPUT_INFO_LABEL_7 = "机箱参数5"

INPUT_INFO_LABEL_8 = "重置"
INPUT_INFO_LABEL_9 = "提交"


READ_INFO_LABEL_0 = """
公告：这个是XZXXXXXX生成的XXXX功能，有着XXXXXX的功能
如何使用？ 
第一步XXXX;
第二步XXXXXX;
更多请咨询电话12345678"""
READ_INFO_LABEL_1 = "打开文件"
READ_INFO_COLUMN_WIDTH = 100



class ReadCsvInfo1(Frame):
	def __init__(self, master):
		Frame.__init__(self, master)
		self.root = master
		self.read_frame = None
		self.createPage()

	def setVisible(self, isVisible):
		if isVisible:
			self.read_frame.pack()
		else:
			self.read_frame.pack_forget()

	def createPage(self):
		self.read_frame = tk.Frame(self.root)
		self.read_frame.pack(pady=20)

		tk.Label(self.read_frame, text=READ_INFO_LABEL_0).grid(row=0, column=0, columnspan=2, pady=10)

		open_button = tk.Button(self.read_frame, text=READ_INFO_LABEL_1, command=self.openFile)
		open_button.grid(row=1, column=0, columnspan=2, pady=10)
		
		self.read_table = ttk.Treeview(self.read_frame)
		self.read_table.grid(row=2) # , fill=tk.BOTH)

	def clearTable(self):
		for item in self.read_table.get_children():
			self.read_table.delete(item)

	def openFile(self):
		file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
		if not file_path:
			return
		try:
			df = pd.read_csv(file_path)
			self.clearTable()

			columns = df.columns.tolist()
			self.read_table['columns'] = columns
			for col in columns:
				self.read_table.heading(col, text=col)
				self.read_table.column(col, width=READ_INFO_COLUMN_WIDTH)

			for index, row in df.iterrows():
				self.read_table.insert("", tk.END, values=list(row))
		except Exception as e:
			tk.messagebox.showerror("错误", f"读取文件时出错: {e}")


class SearchInfo1(Frame):
	def __init__(self, master):
		Frame.__init__(self, master)
		self.root = master
		self.search_frame = None
		self.search_entry = None
		self.createPage()

	def setVisible(self, isVisible):
		if isVisible:
			self.search_frame.pack()
		else:
			self.search_frame.pack_forget()

	def createPage(self):
		search_frame = tk.Frame(self.root)
		search_frame.pack(pady=20)

		tk.Label(search_frame, text=SERCH_INFO_LABEL_0).grid(row=0, column=0, columnspan=2, pady=10)

		self.search_entry = tk.Entry(search_frame, width=SERCH_INPUT_ENTRY_WIDTH)
		self.search_entry.grid(row=1, column=0, padx=10, pady=10)

		self.search_button = tk.Button(search_frame, text=SERCH_INFO_LABEL_2, command=self.doSearch)
		self.search_button.grid(row=1, column=1, padx=10, pady=10)

		self.search_table = ttk.Treeview(search_frame, columns=SEARCH_INFO_TABLE_COLUMNS, show="headings")
		self.search_table.grid(row=2)
		for col in SEARCH_INFO_TABLE_COLUMNS:
			self.search_table.heading(col, text=col)
			self.search_table.column(col, width=SEARCH_INFO_TABLE_COLUMN_WIDTH)

		self.search_frame = search_frame

	def InsertData(self):
		data = self.getData()
		for row in data:
			self.search_table.insert("", tk.END, values=row)

	def clearData(self):
		for item in self.search_table.get_children():
			self.search_table.delete(item)

	def doSearch(self):
		self.clearData()
		self.InsertData()


	def getData(self):
		search_key = self.search_entry.get()
		print("====== getData ==== ", search_key) 

		data = [
			[1, "张三", 12, 190],
			[2, "李四", 21, 110],
			[3, "王五", 52, 120],
			[4, "赵六", 72, 130],
			[5, "朱奇", 22, 80],
		]
		return data



class InputInfo1(Frame):
	def __init__(self, master):
		Frame.__init__(self, master)
		self.root = master
		self.input_frame = None
		self.input_frame_entry1 = None
		self.input_frame_entry2 = None
		self.input_frame_entry3 = None
		self.input_frame_entry4 = None
		self.input_frame_entry5 = None
		self.createPage()

	def setVisible(self, isVisible):
		if isVisible:
			self.input_frame.pack()
		else:
			self.input_frame.pack_forget()

	def createPage(self):
		input_frame = tk.Frame(self.root)
		input_frame.pack(pady=20)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_0).grid(row=0, column=0, columnspan=2, pady=10)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_1).grid(row=1, column=0, padx=10, pady=10)
		self.input_frame_entry1 = tk.Entry(input_frame)
		self.input_frame_entry1.grid(row=1, column=1, padx=10, pady=10)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_2).grid(row=2, column=0, padx=10, pady=10)
		self.input_frame_entry2 = tk.Entry(input_frame)
		self.input_frame_entry2.grid(row=2, column=1, padx=10, pady=10)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_3).grid(row=3, column=0, padx=10, pady=10)
		self.input_frame_entry3 = tk.Entry(input_frame)
		self.input_frame_entry3.grid(row=3, column=1, padx=10, pady=10)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_4).grid(row=4, column=0, padx=10, pady=10)
		self.input_frame_entry4 = tk.Entry(input_frame)
		self.input_frame_entry4.grid(row=4, column=1, padx=10, pady=10)

		tk.Label(input_frame, text=INPUT_INFO_LABEL_5).grid(row=5, column=0, padx=10, pady=10)
		self.input_frame_entry5 = tk.Entry(input_frame)
		self.input_frame_entry5.grid(row=5, column=1, padx=10, pady=10)

		tk.Button(input_frame, text=INPUT_INFO_LABEL_8, command=self.reset_func).grid(row=6, column=0, columnspan=2, pady=10)
		tk.Button(input_frame, text=INPUT_INFO_LABEL_9, command=self.submit_func).grid(row=7, column=0, columnspan=2, pady=10)

		self.input_frame = input_frame

	def reset_func(self):
		code1 = self.input_frame_entry1.get()
		code2 = self.input_frame_entry2.get()
		code3 = self.input_frame_entry3.get()
		code4 = self.input_frame_entry4.get()
		code5 = self.input_frame_entry5.get() 

		print("-=-=====  ", code1, code2, " === ", code3, code4, code5)

		self.input_frame_entry1.delete(0, tk.END)
		self.input_frame_entry2.delete(0, tk.END)
		self.input_frame_entry3.delete(0, tk.END)
		self.input_frame_entry4.delete(0, tk.END)
		self.input_frame_entry5.delete(0, tk.END)

	def submit_func(self):
		code1 = self.input_frame_entry1.get()
		code2 = self.input_frame_entry2.get()
		code3 = self.input_frame_entry3.get()
		code4 = self.input_frame_entry4.get()
		code5 = self.input_frame_entry5.get() 
		print("-=-=====  ", code1, code2, " === ", code3, code4, code5)



class MainPage(object):
	def __init__(self, root):
		self.root = root
		self.root.geometry(MAIN_PAGE_SIZE)
		self.canves_list = []
		self.cur_selected_canves_idx = 0
		self.menu_list = []
		self.creatMenus()
		self.createPage()
		self.showSelectPage(0)

	def creatMenus(self):
		root_menubar = Menu(self.root)
		for menu_item in MENU_LIST:
			label_name = menu_item[0]
			cur_menu = Menu(root_menubar, tearoff=0)
			for idx in range(1, len(menu_item)):
				item_name, item_idx = menu_item[idx]
				cur_menu.add_command(label=item_name, command=lambda idx=item_idx:self.selectMenu(idx))
			root_menubar.add_cascade(label=label_name, menu=cur_menu)
		self.root['menu'] = root_menubar


	def selectMenu(self, idx):
		print("=== selectMenu ", idx)
		self.showSelectPage(idx - 1)

	def createPage(self):
		self.menu_list = [
			SearchInfo1(self.root),
			InputInfo1(self.root),
			ReadCsvInfo1(self.root),
		]

	def showSelectPage(self, idx):
		print("======== showSelectPage ", idx)
		for i, page in enumerate(self.menu_list):
			self.menu_list[i].setVisible(bool(idx == i))



window=tk.Tk()
window.title(WINDOW_TITLE)
MainPage(window)
window.mainloop()
